<template>
	<view style="position: relative;">
		<!-- 产品信息 -->
		<ProductInformation v-if="active == 0 && productData" :data="productInformation" :productData="productData" :goodsCd="goodsCd"></ProductInformation>
		<!-- 溯源信息 -->
		<TraceabilityInformation ref='TraceabilityInformation' v-if="active == 1 && traceabilityId" :goodsCd="goodsCd"
		 :parentId="sourceId" :traceabilityId="traceabilityId"></TraceabilityInformation>
		<!-- 企业信息 -->
		<EnterpriseInformation ref='EnterpriseInformation' v-if="active == 2 && enterpriseInformation" :goodsCd="goodsCd"
		 :enterpriseInformation="enterpriseInformation"></EnterpriseInformation>

		<tabBarView :active="active" @event="(e) => active = e" :goodsCd="goodsCd"></tabBarView>
	</view>

</template>

<script>
	import ProductInformation from './components/productInformation.vue';
	import TraceabilityInformation from './components/traceabilityInformation.vue';
	import EnterpriseInformation from './components/enterpriseInformation.vue';
	export default {
		components: {
			ProductInformation,
			TraceabilityInformation,
			EnterpriseInformation,
		},
		data() {
			return {
				active: 0,
				goodsCd: null,
				productData: null,
				/* 产品信息 数据 */
				productInformation: {
					/* 信息列表 */
					msgListData: [{
							field: 'permitCode',
							title: '生产许可证',
							value: ''
						},
						{
							field: 'productDate',
							title: '产品保质期',
							value: ''
						},
						{
							field: 'productContain',
							title: '产品净含量',
							value: ''
						},
						{
							field: 'productWhere',
							title: '存储条件',
							value: ''
						},
						{
							field: 'productDescribe',
							title: '产品描述',
							value: ''
						},
					],
					/* 厂商列表 */
					manufacturerData: [{
							field: 'name',
							title: '厂商名称',
							value: ''
						},
						{
							field: 'phone',
							title: '厂商电话',
							value: ''
						},
						{
							field: 'address',
							title: '厂商地址',
							value: ''
						},
					],
				},
				/* 溯源信息 数据 */
				traceabilityId: '',
				/* 企业信息 */
				enterpriseInformation: null,
				coverPicture: null,
				sourceId: null
			}
		},
		onLoad(options) {
			// "SPU421823365723194351" || 
			this.goodsCd = options.goodsCd || ''
			this.active = options.active - 0 || 0
			this.coverPicture = options.coverPicture
			this.sourceId = options.sourceId || ''
			this.getRequest()
		},
		onPullDownRefresh() {
			this.active == 0 ? this.getRequest() : this.active == 1 ? this.$refs['TraceabilityInformation'].infoRequest() : this
				.$refs['EnterpriseInformation'].infoRequest()
		},
		methods: {
			getRequest() {
				this.$api.showLoading()
				this.$api.globalUtil.requestAjax({
					url: '/goodsSourceApi/goodsSource',
					method: "POST",
					data: {
						goodsCd: this.goodsCd || '',
						id: this.sourceId || ''
					},
					success: (res) => {
						if (res.data.success) {
							this.productData = res.data.response['producerResultFrom'] ? { ...res.data.response,
								...res.data.response['producerResultFrom']
							} : res.data.response
							this.msgListDataMethod(this.productData)
							this.manufacturerDataMethod(this.productData)
							/* 企业信息 获取 生产商主键 */
							this.enterpriseInformation = {
								id: res.data.response['producerResultFrom'].id
							}
							this.traceabilityId = res.data.response.id
							this.productData.coverPicture = this.coverPicture || ''
							this.productData.sourceId = this.sourceId || ''
							// this.active = this.sourceId ? 1 : 0;
						}
					},
					complate: () => (uni.stopPullDownRefresh(), this.$api.hideLoading())
				});
			},
			// 信息列表 数据便利 函数
			msgListDataMethod(obj) {
				this.productInformation.msgListData.forEach(item => item.value = obj[item.field])
			},
			// 信息列表 数据便利 函数
			manufacturerDataMethod(obj) {
				this.productInformation.manufacturerData.forEach(item => item.value = obj[item.field])
			},
		}
	}
</script>

<style scoped lang="scss">
	.ba-view {
		position: fixed;
		bottom: 0;
		width: 100%;

		.ba {
			padding: 20upx;
			background: #FFFFFF;
			border-radius: 15upx;
		}
	}
</style>
